<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Http\Model\Comment;
use App\Http\Model\CommentImage;
use App\Http\Model\OrdersInfo;
use App\Http\Model\Orders;
use App\Http\Model\RefundImage;
use Intervention\Image\Facades\Image;

class CommentController extends Controller{
	public function comment($goods_id, $orders_info_id){
		return view("home.comment.comment", ["goods_id" => $goods_id, "orders_info_id" => $orders_info_id]);
	}

	// 评论处理
	public function commentRes(Request $request){
		// dd($request->all());
		$ordersInfo = new OrdersInfo;
		$ordersId = $request->input("orders_info_id");
		$ordersInfoData = $ordersInfo->find($ordersId);
		$ordersInfoData->comment_status = 1;
		$ordersInfoData->save();
		if($ordersInfo->where("orders_id", $ordersInfoData->orders_id)->where("comment_status", 0)->count() == 0){
			$orders = new Orders;
			$ordersData = $orders->find($ordersInfoData->orders_id);
			$ordersData->status = 5;
			$ordersData->save();
		}
		$comment = new Comment;
		$comment->content = $request->input("content");
		$comment->user_id = session("uid");
		$comment->goods_id = $request->input("goods_id");
		$comment->orders_info_id = $ordersId;
		// $comment->anonymous = $request->input("anonymous");
		$comment->star = $request->input("star");
		$comment->save();
		$urlArr = explode(",", $request->input("url"));
		if(count($urlArr) && $request->input("url") != ''){
			foreach ($urlArr as $k => $v) {
				$commentImage = new CommentImage;
				$commentImage->comment_id = $comment->id;
				$commentImage->url = $v;
				$commentImage->save();
			}
		}
		return redirect('/orders#tab5')->with('success','评论成功');
	}

	// 评论图片上传
	public function commentUploadImage(Request $request){
		$fileUrl = Array();
		$count = 0;
		$num = $request->input("num");
		if ($request->hasFile('file_all')) {
			foreach ($request->file('file_all') as $k => $v) {
				if ($v->isValid() && $count < $num) {
				    $destinationPath = "./upload/goods/comment/";
	            	$fileName = time().rand(100000,999999).'.'.$v->getClientOriginalExtension();
	            	// $image = Image::make($v);
                    // $image->resize($image->width()*0.2,$image->hight()*0.2)->save($destinationPath.$fileName);
                    $v->move($destinationPath, $fileName);
	            	array_push($fileUrl, "/upload/goods/comment/".$fileName);
	            	$count++;
	            }
			}
		}
		echo json_encode($fileUrl);die;
	}

	// 评论图片删除
	public function deleteImg(Request $request){
		$url = ".".$request->input("src");
		if(file_exists($url)){
			@unlink($url);
		}
		echo 1;die;
	}

	// 申请退款
	public function refund($id){
		return view("home.comment.refund", ["id"=>$id]);
	}

	// 处理申请退款
	public function refundRes(Request $request){
		$ordersInfo = new OrdersInfo;
		$id = $request->input("orders_info_id");
		$ordersInfoData = $ordersInfo->find($id);
        $ordersInfoData->refund_status = 1;
        $ordersInfoData->refund_message = $request->input("content");
        $ordersInfoData->save();
		foreach (explode(",", $request->input("url")) as $k => $v) {
			$refundImage = new RefundImage;
			$refundImage->orders_info_id = $id;
			$refundImage->url = $v;
			$refundImage->save();
		}
		return redirect('/orders')->with('success','申请退款成功');
	}

	// 申请退款图片
	public function refundUploadImage(Request $request){
		$fileUrl = Array();
		$count = 0;
		$num = $request->input("num");
		if ($request->hasFile('file_all')) {
			foreach ($request->file('file_all') as $k => $v) {
				if ($v->isValid() && $count < $num) {
				    $destinationPath = "./upload/goods/refund/";
	            	$fileName = time().rand(100000,999999).'.'.$v->getClientOriginalExtension();
	            	$v->move($destinationPath, $fileName);
	            	array_push($fileUrl, "/upload/goods/refund/".$fileName);
	            	$count++;
	            }
			}
		}
		echo json_encode($fileUrl);die;
	}

	public function refundFail($id){
		$ordersInfo = new OrdersInfo;
		$ordersInfoData = $ordersInfo->find($id);
		// dd($ordersInfoData);
		return view("home.comment.refund-fail", ["ordersInfo"=>$ordersInfoData]);
	}
}